package cn.iocoder.yudao.module.famous.doctor.controller.admin.question;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.question.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.question.FamQuestionDO;
import cn.iocoder.yudao.module.famous.doctor.convert.question.FamQuestionConvert;
import cn.iocoder.yudao.module.famous.doctor.service.question.FamQuestionService;

@Tag(name = "管理后台 - 常见问题")
@RestController
@RequestMapping("/famous.doctor/fam-question")
@Validated
public class FamQuestionController {

    @Resource
    private FamQuestionService famQuestionService;

    @PostMapping("/create")
    @Operation(summary = "创建常见问题")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:create')")
    public CommonResult<Long> createFamQuestion(@Valid @RequestBody FamQuestionCreateReqVO createReqVO) {
        return success(famQuestionService.createFamQuestion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新常见问题")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:update')")
    public CommonResult<Boolean> updateFamQuestion(@Valid @RequestBody FamQuestionUpdateReqVO updateReqVO) {
        famQuestionService.updateFamQuestion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除常见问题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:delete')")
    public CommonResult<Boolean> deleteFamQuestion(@RequestParam("id") Long id) {
        famQuestionService.deleteFamQuestion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得常见问题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:query')")
    public CommonResult<FamQuestionRespVO> getFamQuestion(@RequestParam("id") Long id) {
        FamQuestionDO famQuestion = famQuestionService.getFamQuestion(id);
        return success(FamQuestionConvert.INSTANCE.convert(famQuestion));
    }

    @GetMapping("/list")
    @Operation(summary = "获得常见问题列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:query')")
    public CommonResult<List<FamQuestionRespVO>> getFamQuestionList(@RequestParam("ids") Collection<Long> ids) {
        List<FamQuestionDO> list = famQuestionService.getFamQuestionList(ids);
        return success(FamQuestionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得常见问题分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:query')")
    public CommonResult<PageResult<FamQuestionRespVO>> getFamQuestionPage(@Valid FamQuestionPageReqVO pageVO) {
        PageResult<FamQuestionDO> pageResult = famQuestionService.getFamQuestionPage(pageVO);
        return success(FamQuestionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出常见问题 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-question:export')")
    @OperateLog(type = EXPORT)
    public void exportFamQuestionExcel(@Valid FamQuestionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FamQuestionDO> list = famQuestionService.getFamQuestionList(exportReqVO);
        // 导出 Excel
        List<FamQuestionExcelVO> datas = FamQuestionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "常见问题.xls", "数据", FamQuestionExcelVO.class, datas);
    }

}
